package com.Mobile.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.Entity.Community;
import com.Entity.Userdata;
import com.Entity.Users;
import com.Service.CommunityService;
import com.Service.UserdataService;
import com.Service.UsersService;

public class MobileDisplayProfileAction implements ServletResponseAware{
//展示个人和社区信息


	private HttpServletResponse response;//response闁诲海鏁搁、濠囨寘?
	private int flag=0;
	@Autowired
	private UsersService usersservice;
	@Autowired
	private UserdataService userdataService;
	@Autowired
	private CommunityService communityservice;
	private Users user=new Users();
	private Userdata userdata=new Userdata();
	private Community community;
	
	public void DisplayProfile() throws ServletException,IOException {
		String userid=ServletActionContext.getRequest().getParameter("UserID");
		userid = java.net.URLDecoder.decode(userid,"UTF-8");
		
		JSONObject returnjson=new JSONObject();
		JSONArray jsonarray=new JSONArray();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		try {
			PrintWriter out = response.getWriter();
			int id=Integer.parseInt(userid);
			user = this.usersservice.getById(id);
			System.out.println(user.getNickName());
			if(user!=null){
				flag=1;	//闂佽皫鍡╁殭缂傚秴绉归獮瀣箛椤掆偓椤?																
				userdata=this.userdataService.getById(id);																
				 ArrayList list = new ArrayList();
				 int CommunityHosID=user.getCommunityId();
				 community=this.communityservice.getById(CommunityHosID);
				 String CommunityHosName=community.getCommunityHosName();
				 String CommunityDesc=community.getCommunityDesc();
				 int Status=community.getStatus();
				 String nickName=user.getNickName();
				 String phoneNumber=userdata.getPhoneNumber(); 
				 String privateEmail=user.getPrivateEmail();
				 list.add("CommunityHosID:"+CommunityHosID);
				 list.add("CommunityHosName:"+CommunityHosName);
				 list.add("CommunityDesc:"+CommunityDesc);
				 list.add("Status:"+Status);
				 list.add("nickName:"+nickName);
				 list.add("phoneNumber:"+phoneNumber);
				 list.add("privateEmail:"+privateEmail);
				 
				 returnjson.put("flag",flag);				 
				 returnjson.put("info",list);
					
				}
				else{
					
					returnjson.put("flag", flag);
				}						
				out.println(returnjson.toString());
				out.flush();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
		
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public Userdata getUserdata() {
		return userdata;
	}

	public void setUserdata(Userdata userdata) {
		this.userdata = userdata;
	}

	public Community getCommunity() {
		return community;
	}

	public void setCommunity(Community community) {
		this.community = community;
	}

}
